from django.shortcuts import render

# Create your views here.

from SearchHospital.ser import DepartmentModelSerializer, HospitalModelSerializer
from SearchHospital.models import DepartmentModel, HospitalModel

from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.filters import OrderingFilter

# 分页配置
class StandardResultsSetPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    max_page_size = 100

# 排序配置
class CustomOrderingFilter(OrderingFilter):
    ordering_params = ('name', 'establishment_year')

# 科室表 ListAPIView
class DepartmentModelListView(ListAPIView):
    serializer_class = DepartmentModelSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['name']

    def get_queryset(self):
        return DepartmentModel.objects.all()

# 医院表 ListAPIView
class HospitalModelListView(ListAPIView):
    serializer_class = HospitalModelSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['name', 'establishment_year']

    def get_queryset(self):
        return HospitalModel.objects.all()

# 医院表 RetrieveAPIView
class HospitalModelDetailView(RetrieveAPIView):
    serializer_class = HospitalModelSerializer
    queryset = HospitalModel.objects.all()
    lookup_field = 'id'